package com.bestcem.xm.user.grpc.client;

import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.survey.grpc.v1.services.BpSurveyServiceGrpc;
import com.bestcem.xm.survey.grpc.v1.services.BpSurveyStatServiceGrpc;
import com.bestcem.xm.survey.grpc.v1.services.SurveyGroupIsInUseRequest;
import com.bestcem.xm.survey.grpc.v1.services.SurveyGroupIsInUseResponse;
import com.bestcem.xm.user.service.dto.user.OrganizationQuotainfoDTO;
import com.zy.dp.proto.base.v1.Code;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

/**
 * 答卷相关grpc服务
 *
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2022/8/5 11:32
 */
@Slf4j
@Service("userBpSurveyQuotaGrpcService")
public class BpSurveyQuotaGrpcService {

    @GrpcClient(GrpcClientNames.DS_SURVEY)
    private BpSurveyStatServiceGrpc.BpSurveyStatServiceBlockingStub stub;

    @GrpcClient(GrpcClientNames.DS_SURVEY)
    private BpSurveyServiceGrpc.BpSurveyServiceBlockingStub bpSurveyServiceBlockingStub;

    /**
     * 获取机构问卷和答卷使用情况
     *
     * @param orgId 组织id
     * @return com.bestcem.xm.user.service.dto.user.OrganizationQuotainfoDTO
     * @author zhangfeng <feng.zhang@idiaoyan.com>
     * @date 2022/8/5 11:38
     */
    public OrganizationQuotainfoDTO getQuotaByOrgId(String orgId) {
        OrganizationQuotainfoDTO quotainfoDTO = new OrganizationQuotainfoDTO();
        // 设置请求参数
//        GetStatByOrgIdRequest request = GetStatByOrgIdRequest.newBuilder().setOrgId(orgId).build();
//        try {
//            // 发送请求
//            GetStatByOrgIdResponse response = stub.getStatByOrgId(request);
//            // 返回值处理
//            if (Code.OK_VALUE == response.getStatus().getCodeValue()) {
//                quotainfoDTO.setProjectTotal(response.getProject());
//                quotainfoDTO.setRespondentTotal(response.getSurvey());
//                return quotainfoDTO;
//            }
//            log.error("[USER] 获取行业负责人失败. orgId: {}", orgId);
//        } catch (Exception e) {
//            log.error("[USER] 获取行业负责人失败. 发生异常.", e);
//        }
        quotainfoDTO.setProjectTotal(0);
        quotainfoDTO.setRespondentTotal(0L);
        return quotainfoDTO;
    }

    public Boolean surveyGroupIsInUse(String groupId) {
        // 设置请求参数
        SurveyGroupIsInUseRequest request = SurveyGroupIsInUseRequest.newBuilder().setGroupId(groupId).build();
        try {
            // 发送请求
            SurveyGroupIsInUseResponse response = bpSurveyServiceBlockingStub.surveyGroupIsInUse(request);
            // 返回值处理
            if (Code.OK_VALUE == response.getStatus().getCodeValue()) {
                return response.getInUse();
            }
            log.error("[USER] 判断指定层级是否有正式答卷提交. groupId: {}", groupId);
        } catch (Exception e) {
            log.error("[USER] 判断指定层级是否有正式答卷提交. 发生异常.", e);
        }
        return null;
    }
}
